package com.tengju.bff.infrastructure.message.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.idanchuang.pay.split.service.facade.entity.res.payment.PaymentRequisitionRes;
import com.idanchuang.pay.split.service.facade.entity.res.payment.mq.PaymentRequisitionMessage;
import com.rabbitmq.client.Channel;
import com.tengju.bff.infrastructure.shared.MqMessageConstants;
import com.tengju.bff.interfaces.covercharge.service.impl.ServiceLetaxWithdrawLogServiceImpl;
import com.tengju.bff.interfaces.covercharge.service.impl.ServiceSelfCertificationWithdrawServiceImpl;
import com.tengju.data.domain.shared.CastUtil;
import com.tengju.payment.domain.model.transfer.CertificationTypeEnum;
import com.tengju.payment.domain.model.transfer.Transfer;
import com.tengju.payment.domain.model.transfer.TransferEvent;
import com.tengju.payment.domain.model.transfer.TransferRecordRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class TransferRevokeConsumer {

    private final TransferRecordRepository transferRecordRepository;
    private final ServiceLetaxWithdrawLogServiceImpl serviceLetaxWithdrawLogService;
    private final ServiceSelfCertificationWithdrawServiceImpl selfCertificationWithdrawService;


    @RabbitHandler
    @RabbitListener(queues = MqMessageConstants.TRANSFER_REVOKE_QUEUE, containerFactory = "rabbitListenerContainerFactory")
    public void receive(@Payload String message, Channel channel, @Headers Map<String, Object> headers) {
        log.info("支付转账消息" + message);
        PaymentRequisitionMessage transferMessage = JSON.parseObject(message, PaymentRequisitionMessage.class);
        if (transferMessage == null || CastUtil.castInt(transferMessage.getEventType()) != TransferEvent.TRANSFER_REVOKE.getCode()) {
            ack(channel, headers);
            return;
        }
        PaymentRequisitionRes result = transferMessage.getBody();
        Transfer transfer = transferRecordRepository.findByTradeNo(result.getRequisitionNo());

        if (CertificationTypeEnum.NO_CERTIFICATION == transfer.certificationType()) {
            serviceLetaxWithdrawLogService.transferRevoke(transfer.getBusinessNo().getValue());
        } else {
            selfCertificationWithdrawService.transferRevoke(transfer.getBusinessNo().getValue());
        }

        ack(channel, headers);


    }

    private void ack(Channel channel, Map<String, Object> headers) {
        try {
            channel.basicAck((Long) headers.get(AmqpHeaders.DELIVERY_TAG), false);
        } catch (IOException exception) {
            log.error("支付转账消息ack异常：" + exception.getMessage());
        }
    }

}
